package com.lovo.pis.utils;


import com.lovo.pis.dto.LogBean;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.mapper.LogMapper;
import com.lovo.pis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class LogUtil {

    @Autowired
    UserMapper userMapper;
    @Autowired
    LogMapper logMapper;

    public void recordLog(int userId, String str) {
        UserBean userBean = userMapper.selectById(userId);
        String realName = userBean.getRealName();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd?HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String nowTimeStr = dtf.format(localDateTime);
        String date = nowTimeStr.split("[?]")[0];
        String time = nowTimeStr.split("[?]")[1];
        String content = realName + time + str;
        LogBean logBean = LogBean.builder().date(date).content(content).build();
        logMapper.insert(logBean);
    }
}
